package cn.edu.cjw.APMFP.Product.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.BOMTable.DAO.BOMTableDAO;
import cn.edu.cjw.APMFP.BOMTable.DAO.BOMTableDAOImpl;
import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Product.pojo.Product;
import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;
import cn.edu.cjw.APMFP.Util.StringBackToMap;

public class ProductDAOImpl implements ProductDAO {

	BOMTableDAO bDao = new BOMTableDAOImpl();

	public boolean addAndUpdateProduct(Product product) throws Exception {
		// TODO Auto-generated method stub
		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(Product.PRODUCT_TABLE_NAME));

		Put putProduct = HBaseUtill.PutProduct(product);

		table.put(putProduct);

		HBaseUtill.Close(table, conn);

		return true;
	}

	public boolean removeProduct(Product product) throws Exception {
		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(Product.PRODUCT_TABLE_NAME));

		Delete delete = new Delete(Bytes.toBytes(product.getBId()));

		table.delete(delete);

		HBaseUtill.Close(table, conn);

		return true;
	}

	public Product searchProductById(String pid) throws Exception {

		Product product = new Product();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_product_table WHERE BId=?";
		PreparedStatement pStatement = conn.prepareStatement(sql);

		pStatement.setString(1, pid);
		ResultSet rs = pStatement.executeQuery();

		while (rs.next()) {

			product.setProduct(bDao.searchById(rs.getString("bid")));
			product.setMaterial(StringBackToMap.KStringVLong(rs.getString("material")));
			product.setWorkstep(StringBackToMap.KIntVString(rs.getString("workstep")));

		}

		HiveUtill.Close(rs, pStatement, conn);
		
		return product;

	}

	public ArrayList<Product> searchProductByname(String name) throws Exception {

		ArrayList<Product> list = new ArrayList<Product>();
		
		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_product_table WHERE BName=?";
		PreparedStatement pStatement = conn.prepareStatement(sql);

		pStatement.setString(1, name);
		ResultSet rs = pStatement.executeQuery();

		while (rs.next()) {

			Product product = new Product();
			BOMTable searchById = bDao.searchById(rs.getString("BId"));
			product.setProduct(searchById);
			product.setMaterial(StringBackToMap.KStringVLong(rs.getString("material")));
			product.setWorkstep(StringBackToMap.KIntVString(rs.getString("workstep")));

			list.add(product);
		}

		HiveUtill.Close(rs, pStatement, conn);
		
		return list;
	}


	@Override
	public ArrayList<Product> showAll() throws Exception {
		ArrayList<Product> list = new ArrayList<Product>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String Hive_url = bundle.getString("Hive-url");
		String Hive_user = bundle.getString("Hive-user");
		String Hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(Hive_url, Hive_user, Hive_password);

		String sql = "SELECT * FROM apmfp_product_table";
		PreparedStatement pStatement = conn.prepareStatement(sql);
		ResultSet rs = pStatement.executeQuery();
		while (rs.next()) {
			Product product = new Product();
			BOMTable searchById = bDao.searchById(rs.getString("BId"));
			product.setProduct(searchById);
			product.setMaterial(StringBackToMap.KStringVLong(rs.getString("material")));
			product.setWorkstep(StringBackToMap.KIntVString(rs.getString("workstep")));

			list.add(product);
		}

		HiveUtill.Close(rs, pStatement, conn);

		return list;
	}
}
